package com.gaohe.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gaohe.common.R;
import com.gaohe.pojo.*;
import com.gaohe.service.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("orders")
@Slf4j
public class OrdersController {
//    查询orders的信息和orders_detail的时间
    @Autowired
    OrderService orderService;

    @GetMapping("/page")
    private R pages(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestParam(required = false) String status)
    {
        PageHelper.startPage(page,pageSize);
        List<Orders>list = orderService.selectAll(status);
        PageInfo<Orders>pageInfo = new PageInfo<>(list);
        return R.success(pageInfo);
    }

//    @GetMapping
//    private R getById(@RequestParam Long userId)
//    {
//        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper();
//        lqw.eq(Orders::getUserId,userId);
//        List<Orders> list = orderService.list(lqw);
//        return R.success(list);
//    }

    /**
     * 下单功能 用户端
     * @param orders
     * @return
     */
    @Autowired
    OrderDetailService orderDetailService;

    @Autowired
    UserService userService;

    @Autowired
    ShopService shopService;
    @PostMapping("/placeOrder")
    public R placeOrder(@RequestBody Orders orders){
        //        1.获取前端传来的参数 userId用户id shopId商品id shopCount商品数量
        Long userId =orders.getUserId();
        log.info("userId:{}",userId);
        Long shopId =orders.getShopId();
        Integer shopCount = orders.getShopCount();
        //        2.根据用户id和商品id获得商品和用户信息
        User user = userService.getById(userId);
        Shop shop = shopService.getById(shopId);
        //        3.判断商品是否在售,若处于在售状态在创建订单
        if (shop.getStatus()==1){
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setCreateTime(LocalDateTime.now());
            orderDetail.setEndTime(LocalDateTime.now());
            orders.setUserId(userId);
            orders.setShopId(shopId);
            orders.setShopCount(shopCount);
            orders.setAccount(shop.getNewPrice()*shopCount);
            orders.setStatus(1);
            orders.setShopName(shop.getShopName());
            orders.setPhone(user.getPhone());
            orders.setImage(shop.getImage());
            orders.setPrice(shop.getNewPrice());
            orders.setOrderId(orderDetail.getId());
            if (orderDetailService.save(orderDetail)&&orderService.save(orders)){
                return R.success("下单成功");
            }else {
                return R.error("下单失败,请联系管理员");
            }
        }else {
            return R.error("商品已停售");
        }
    }
    /**
     * 根据用户id模糊查询订单列表 用户端
     * @param userId
     * @param shopName
     * @return
     */
    @GetMapping
    public R list(Long userId,  String shopName){
        LambdaQueryWrapper<Orders> qw = new LambdaQueryWrapper<>();
        qw.eq(Orders::getUserId,userId);
        qw.like(!StringUtils.isEmpty(shopName),Orders::getShopName,shopName);
        return R.success(orderService.list(qw));
    }

    /**
     * 发表评论 用户端
     * @param comment
     * @return
     */
    @Autowired
    CommentService commentService;
    @PostMapping("/submitComment")
    public R submitComment(@RequestBody Comment comment){
        comment.setCreateTime(LocalDate.now());
        return commentService.save(comment)?R.success("评论成功"):R.error("评论失败");
    }

}
